<template>
  <div class="sharedetail">
    <van-nav-bar title="预览海报" @click-left="$router.back()" left-arrow fixed>
      <div slot="right" @click="showAction = true">
        <van-icon name="ellipsis" size="20" />
      </div>
    </van-nav-bar>
    <div
      class="content"
      :style="{
        height: infoData.card ? 'calc(100vh - 46vw)' : 'calc(100vh - 15vw)',
      }"
    >
      <h3>{{ infoData.title }}</h3>
      <img :src="$publicObj.urlImg(infoData.content)" alt="" />
      <p v-html="infoData.explains"></p>
      <span class="conspan">{{ infoData.copyright }}</span>
      <!-- 公众号 -->
      <div class="public" v-if="infoData.attention">
        <img :src="userInfo.officialChatImage" alt="" />
      </div>
      <!-- 点赞收藏浏览量 -->
      <div class="foot">
        <span @click="like = !like">
          <i
            class="iconfont"
            :class="{ 'icon-dianzan1': like, 'icon-dianzan': !like }"
            :style="{ color: like ? 'red' : '' }"
          ></i>
          点赞
        </span>
        <span @click="collect = !collect">
          <i
            class="iconfont"
            :class="{ 'icon-shoucang1': collect, 'icon-shoucang': !collect }"
            :style="{ color: collect ? 'red' : '' }"
          ></i>
          收藏
        </span>
        <span>
          <i class="iconfont icon-liulan"></i>
          浏览 {{ infoData.score }}
        </span>
      </div>
      <van-divider />
      <!-- 提交表单 -->
      <div class="form" v-show="infoData.form && !infoData.notForm">
        <h3>填写信息 申请服务</h3>
        <div class="forms">
          <div class="item">
            <label><span>*</span>姓名</label>
            <input
              type="text"
              placeholder="您的信息将严格被保密"
              v-model="name"
              disabled
            />
          </div>
          <div class="item">
            <label><span>*</span>电话</label>
            <input
              type="text"
              placeholder="您的信息将严格被保密"
              v-model="phone"
              disabled
            />
          </div>
          <div class="item">
            <label><span>*</span>验证码</label>
            <input
              type="text"
              placeholder="请输入验证码"
              disabled
              v-model="code"
            />
            <span class="code">获取验证码</span>
          </div>
          <div class="item">
            <label>备注</label>
            <textarea
              placeholder="您的信息将严格被保密"
              disabled
              v-model="remark"
            />
          </div>
        </div>
        <div class="btn">提交</div>
      </div>
      <div class="forms" v-show="infoData.form && infoData.notForm">
        <img src="../../assets/images/detail/detail_form.png" alt="" />
      </div>

      <div class="user" v-if="infoData.card">
        <img :src="userInfo.image" class="userimg" />
        <div class="xinxi">
          <div class="rights">
            <div class="right">
              <h4>分享人：{{ userInfo.nickname }}</h4>
              <span>
                <img src="../../assets/images/detail/ewm_y.png" alt="" />
                微信号：{{ userInfo.weChatNo || "" }}
              </span>
              <span>
                <img src="../../assets/images/detail/ewm_y.png" alt="" />
                手机号：{{ userInfo.phone }}
              </span>
            </div>
            <!-- v-if="infoData.shareWeChatImage" -->
            <div class="ewm">
              <img :src="userInfo.weChatImage" alt="" />
            </div>
          </div>
          <div class="foots">
            <p>长按可识别二维码</p>
          </div>
        </div>
      </div>
    </div>
    <!-- 弹框 -->
    <van-action-sheet
      v-model="showAction"
      :actions="actions"
      cancel-text="取消"
      close-on-click-action
      @cancel="showAction = false"
      @select="select"
    />
    <!-- 删除弹框 -->
    <van-dialog v-model="showDel" :show-confirm-button="false">
      <template #title>
        <van-icon name="cross" @click="showDel = false" />
      </template>
      <template #default>
        <div class="cont">
          <p>确定要删除该海报？</p>
          <van-divider />
          <div class="foot">
            <div class="left" @click="showDel = false">取消</div>
            <div class="right" @click="deleteArticlesEtc">确定</div>
          </div>
        </div>
      </template>
    </van-dialog>
    <!-- 弹框 -->
    <div class="overlay" v-show="show"></div>
    <div class="dialog" v-show="show">
      <img
        src="../../assets/images/detail/detail_img.png"
        alt=""
        class="dialog_img"
      />
      <h3>完善个人信息</h3>
      <p>完善信息后，可以将完善的信息在 分享海报中显示。</p>
      <div class="foot">
        <button @click="perfectClick(1)">去完善</button>
        <button @click="perfectClick(2)">直接分享</button>
      </div>
      <div class="close" @click="show = false">
        <img src="../../assets/images/detail/detail_close.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      id: "",
      infoData: {},
      name: "", //姓名
      phone: "", //手机号
      remark: "", //备注
      code: "", //验证码
      count: 60, //倒计时
      timer: null,
      isCode: false,
      like: false, //点赞
      collect: false, //收藏
      type: "", //类型
      showAction: false, //选择弹框
      showDel: false, //删除弹框
      show: false, //完善信息弹框
    };
  },
  created() {},

  async mounted() {
    await this.$store.dispatch("updateUserInfo");
    this.userInfo = this.$store.state.userInfo;
    // 海报数据
    this.getPosterInfo();
  },
  computed: {
    actions() {
      let arr = [
        { name: this.infoData.status == 1 ? "上架海报" : "下架海报", id: 2 },
        { name: "编辑文章或修改推广设置", id: 4 },
        { name: "删除海报", id: 5 },
        { name: "立即分享", id: 3 },
      ];
      // if (this.infoData.type == 8) {
      //   arr.unshift({ name: "推广设置", id: 1 });
      // }
      return arr;
    },
  },
  methods: {
    //选择
    select(e) {
      if (e.id == 5) {
        //删除
        if (this.isRoot() == 2) {
          return this.$toast("您的权限不足");
        }
        this.showDel = true;
      } else if (e.id == 4) {
        //修改海报
        if (this.isRoot() == 2) {
          return this.$toast("您的权限不足");
        }
        this.$router.push(`/addPoster?id=${this.infoData.id}&title=修改`);
      } else if (e.id == 2) {
        // 上下架
        this.updateStatus(this.infoData.status, this.infoData.id);
      } else if (e.id == 3) {
        // 分享
        this.clickShare();
      } else if (e.id == 1) {
        //推广海报
        this.$router.push(`/addPoster?id=${this.infoData.id}&title=推广`);
      }
    },
    //完善分享
    perfectClick(n) {
      let ids = this.userInfo.id;
      this.show = false;
      if (n == 1) {
        this.$router.push("/personal?post=" + this.$route.query.id);
      } else {
        this.$router.push({
          path: `/postShareEdit?id=${this.$route.query.id}&userId=${ids}&tableId=${this.$route.query.id}`,
        });
      }
    },
    //选择分享
    async clickShare() {
      if (this.type == 9) {
        //判断是否有分享次数
        let obj = {
          id: this.$route.query.id,
          tableType: 3,
          type: 9,
        };
        var result = await this.$api.checkingSpreadShare(obj);
      }
      if (this.type == 9 && !result.status) {
        return this.$toast("您不能分享该海报！");
      }

      let ids = this.userInfo.id;
      let res = await this.$api.checkUserInfo(ids);
      if (res.code != 0) {
        this.show = true;
        return;
      }

      this.$router.push({
        path: `/postShareEdit?id=${this.$route.query.id}&userId=${ids}&tableId=${this.$route.query.id}`,
      });
    },
    //上下架
    async updateStatus(status, id) {
      var str;
      if (status == 1) {
        str = "上架";
        status = 2;
      } else {
        str = "下架";
        status = 1;
      }
      let obj = {
        tableType: 3,
        status,
        id,
        type: 2, //上下架 1审核2上下架
      };
      let res = await this.$api.updateStatus(obj);

      await this.getPosterInfo();
      if (res.code != 0) return this.$toast(res.msg);
      this.$toast(str + "成功");
      setTimeout((_) => {
        //返回上一级页面并且刷新页面
        // this.$publicObj.back();
        this.$router.back();
      }, 800);
    },
    // 判断是否有权限删除修改等
    async isRoot() {
      let res = await this.$api.isCustomer({
        openId: this.userInfo.openId,
        account: this.userInfo.account,
        id: this.userInfo.id,
        phone: this.userInfo.phone,
        type: 1,
        tableType: 1,
        tableId: this.infoData.id,
      });
      return res.data;
    },
    //删除
    async deleteArticlesEtc() {
      let obj = {
        id: this.infoData.id,
        tableType: 3,
        userId: this.$store.state.userInfo.id,
      };
      let res = await this.$api.deleteArticlesEtc(obj);
      if (res.code != 0) return this.$toast(res.msg);
      this.showDel = false;
      await this.getPosterInfo();
      this.$toast("删除成功");
      setTimeout((_) => {
        //返回上一级页面并且刷新页面
        // this.$publicObj.back();
        this.$router.back();
      }, 800);
    },
    //获取海报数据
    async getPosterInfo() {
      let userInfo = JSON.parse(sessionStorage.getItem("userWxData"));
      let {
        openid,
        nickname: name,
        headimgurl: image,
        sex,
        city,
        province,
      } = userInfo;
      this.tableId = Number(this.$route.query.id);
      let obj = {
        tableId: this.$route.query.id,
        openId: openid,
        name,
        image,
        sex,
        city,
        province,
        userId: this.$store.state.userInfo.account,
        tableType: 3,
      };
      let res = await this.$api.getArticleInfo(obj);
      if (res.code != 0) return;
      this.infoData = res.data;
      this.type = res.data.type;
      sessionStorage.setItem("changeTitle", res.data.changeTitle);
      sessionStorage.setItem("description", res.data.description);
      sessionStorage.setItem("title", res.data.title);
      sessionStorage.setItem("article_img", res.data.cover);
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .van-dialog__header {
  text-align: right;
  padding: 15px 15px 0 0;
}
.van-dialog {
  width: 272px;
  .cont {
    font-size: 14px;
    font-weight: 700;
    color: #353535;
    text-align: center;
    p {
      padding: 20px 0;
    }
    .van-divider {
      margin: 0;
    }
    .foot {
      width: 100%;
      height: 43px;
      display: flex;
      .left,
      .right {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 14px;
        color: #999999;
      }
      .right {
        color: #009dff;
        border-left: 1px solid #f5f6f8;
      }
    }
  }
}
.content {
  box-sizing: border-box;
  // height: calc(100vh - 215px);
  padding: 66px 15px 20px;
  position: relative;
  overflow-y: scroll;
  h3 {
    font-size: 22px;
    color: #212121;
    margin: 16px 0 11px;
    text-align: center;
    font-weight: 700;
  }
  p {
    font-size: 11px;
    color: #999999;
    line-height: 21px;
  }
  img {
    width: 100%;
  }
  .conspan {
    float: right;
    font-size: 11px;
    color: #666666;
  }
  .public {
    margin-top: 35px;
    position: relative;
    background: url("../../assets/images/detail/zzh_bg.png") no-repeat;
    background-size: 100%;
    width: 100%;
    height: 138px;
    img {
      position: absolute;
      width: 87px;
      height: 87px;
      right: 60px;
      top: 30px;
    }
  }
  .foot {
    margin-top: 20px;
    display: flex;
    display: -webkit-flex;
    align-items: center;
    font-size: 11px;
    color: #6c7592;
    justify-content: space-between;
    span {
      display: flex;
      align-items: center;
      i {
        margin-right: 4px;
      }
      &:nth-of-type(2) {
        margin-right: 155px;
      }
    }
  }
  .form {
    height: 350px;
    background-color: #fbfcff;
    box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.16);
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 20px;

    h3 {
      background: url("../../assets/images/detail/head_g.png") no-repeat;
      background-size: contain;
      background-position: center;
      width: 178px;
      font-size: 17px;
      font-weight: 700;
      text-align: center;
      color: #000000;
      margin-top: 18px;
    }
    .forms {
      margin-top: 14px;
      padding: 0 15px;
      box-sizing: border-box;
      .item {
        display: flex;
        align-items: center;
        margin-bottom: 15px;
        position: relative;
        .code {
          position: absolute;
          right: 6px;
          top: 6px;
          display: flex;
          justify-content: center;
          align-items: center;
          width: 60px;
          height: 21px;
          background-color: #ff8571;
          border-radius: 5px;
          font-size: 11px;
          color: #ffffff;
        }
        label {
          font-size: 14px;
          color: #000000;
          width: 63px;
          height: 20px;
          margin-right: 24px;
          span {
            color: red;
          }
        }
        input {
          font-size: 12px;
          padding: 8px 0 9px 16px;
          width: 227px;
          box-sizing: border-box;
          height: 34px;
          background: #ffffff;
          border: 1px solid #999999;
          border-radius: 5px;
          &::placeholder {
            color: #999999;
          }
        }
        textarea {
          font-size: 12px;
          padding: 8px 0 9px 16px;
          box-sizing: border-box;
          width: 228px;
          height: 91px;
          background-color: #ffffff;
          border: 1px solid #999999;
          border-radius: 5px;
          &::placeholder {
            color: #999999;
          }
        }
      }
    }
    .btn {
      width: 100%;
      height: 40px;
      background-color: #0264d1;
      border-radius: 0px 0px 5px 5px;
      font-size: 14px;
      font-weight: 700;
      color: #ffffff;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .forms {
    width: 100%;
    height: 270px;
    img {
      width: 100%;
      height: 100%;
    }
  }

  .user {
    background-color: #edf1f4;
    display: flex;
    padding: 0 4vw;
    background: url("../../assets/images/detail/ewm_bg.png") no-repeat;
    background-size: 100% 100%;
    box-sizing: border-box;
    height: 140px;
    width: 345px;
    position: fixed;
    left: 15px;
    bottom: 50px;
    color: #e2e2e2;
    .userimg {
      width: 84px;
      height: 84px;
      margin: 33px 0 0 12px;
      border-radius: 5px;
    }
    .xinxi {
      display: flex;
      flex-direction: column;
      flex: 1;
      .rights {
        display: flex;
        .right {
          margin-left: 35px;
          padding-top: 44px;
          h4 {
            font-size: 11px;
            margin-bottom: 12px;
          }
          span {
            display: flex;
            align-items: center;
            font-size: 9px;
            margin-bottom: 8px;
            line-height: normal;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            img {
              width: 8px;
              height: 8px;
            }
          }
        }
        .ewm {
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: flex-end;
          align-items: center;
          margin-left: 32px;
          img {
            margin-bottom: 1px;
            width: 43px;
          }
        }
      }
      .foots {
        width: 100%;
        margin-top: 4px;
        p {
          font-size: 8px;
          color: #ffffff;
          text-align: right;
        }
      }
    }
  }
}

.img {
  width: 14px;
  height: 14px;
  margin-right: 4px;
}
.share {
  font-size: 14px;
  color: #707070;
}
.dialog {
  position: fixed;
  z-index: 2002;
  top: 50%;
  left: 50%;
  -webkit-transform: translate3d(-50%, -50%, 0);
  transform: translate3d(-50%, -50%, 0);
  width: 268px;
  height: 271px;
  background: url("../../assets/images/detail/detail_bg.png") no-repeat;
  background-size: 100% 100%;
  .dialog_img {
    width: 124px;
    height: 127px;
    position: absolute;
    left: 71.5px;
    top: -47px;
    z-index: 2003;
  }
  h3 {
    font-size: 16px;
    font-weight: 700;
    color: #333333;
    margin-top: 120px;
    margin-bottom: 10px;
    text-align: center;
  }
  p {
    font-size: 14px;
    color: #666666;
    padding: 0 29px;
  }
  .foot {
    margin-top: 34px;
    padding: 0 18px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    button {
      width: 90px;
      height: 32px;
      background: linear-gradient(90deg, #fcaf36 1%, #fe774c);
      border-radius: 16px;
      color: #fff;
      border: none;
      &:last-of-type {
        border: 1px solid #fd7c49;
        color: #fd7c4a;
        background: #fff;
      }
    }
  }
  .close {
    width: 35px;
    height: 35px;
    position: absolute;
    bottom: -50px;
    left: 116px;
    z-index: 2004;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  background-color: rgba(90, 90, 90, 0.5);
}
</style>
